package top.drinyeit.nongmao.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.presenter.CategoryPresenter;

/**
 * Created by wu on 2018/1/9.
 */

public class CategoryActivity extends BaseActivity{

    private TextView price,sold;
    private RelativeLayout price_sort,sold_sort;
    public RecyclerView mRecyclerView;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;
    private CategoryPresenter categoryPresenter;
    private int category_id;
    private String categoryName;
    private int priceNo = 0;
    private int soldNo = 0;
    public int pageIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        initIntent();
        initView();
        initData();
        initListener();
    }

    public void initIntent(){
        Bundle bundle =getIntent().getExtras();
        category_id = bundle.getInt("category_id");
        categoryName = bundle.getString("category_name");
    }

    public void initView(){
        price = findViewById(R.id.price);
        sold = findViewById(R.id.sold);
        price_sort = findViewById(R.id.price_sort);
        sold_sort = findViewById(R.id.sold_sort);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        shSwipeRefreshLayout = findViewById(R.id.srl_category_result);
    }

    public void initData(){
        categoryPresenter = new CategoryPresenter(this);
        Map<String,Object> map = new HashMap<>();
        map.put("action","commodity_category_goods");
        map.put("category_id",category_id);
        map.put("page_index", 1);
        categoryPresenter.categorySearch(map);
    }

    public void initListener(){
        shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                Map<String,Object> map = new HashMap<>();
                map.put("action","commodity_category_goods");
                map.put("category_id",category_id);
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", 1);
                categoryPresenter.categorySearch(map);
            }

            @Override
            public void onLoading() {
                Map<String,Object> map = new HashMap<>();
                map.put("action","commodity_category_goods");
                map.put("category_id",category_id);
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", pageIndex + 1);
                categoryPresenter.categorySearch(map);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
        price_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                priceSort();
            }
        });
        sold_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soldSort();
            }
        });
    }

    private void priceSort(){
        sold.setTextColor(ContextCompat.getColor(this,R.color.text_black_51));
        sold.setCompoundDrawables(null,null,null,null);
        Map<String,Object> map = new HashMap<>();
        map.put("action","commodity_category_goods");
        map.put("category_id",category_id);
        Drawable drawable0= ContextCompat.getDrawable(this,R.drawable.up_arrow_icon);
        Drawable drawable1= ContextCompat.getDrawable(this,R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (priceNo == 0){
            map.put("price_type",1);
            map.put("page_index", 1);
            categoryPresenter.categorySearch(map);
            price.setTextColor(ContextCompat.getColor(this,R.color.app_green));
            price.setCompoundDrawables(null,null,drawable0,null);
            priceNo = 1;
            soldNo = 0;
        }else if (priceNo == 1){
            map.put("price_type",2);
            map.put("page_index", 1);
            categoryPresenter.categorySearch(map);
            price.setCompoundDrawables(null,null,drawable1,null);
            priceNo = 2;
        }else {
            map.put("price_type",1);
            map.put("page_index", 1);
            categoryPresenter.categorySearch(map);
            price.setCompoundDrawables(null,null,drawable0,null);
            priceNo = 1;
        }
    }

    private void soldSort(){
        price.setTextColor(ContextCompat.getColor(this,R.color.text_black_51));
        price.setCompoundDrawables(null,null,null,null);
        Map<String,Object> map = new HashMap<>();
        map.put("action","commodity_category_goods");
        map.put("category_id",category_id);
        Drawable drawable0= ContextCompat.getDrawable(this,R.drawable.up_arrow_icon);
        Drawable drawable1= ContextCompat.getDrawable(this,R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (soldNo == 0){
            map.put("sell_type",1);
            map.put("page_index", 1);
            categoryPresenter.categorySearch(map);
            sold.setTextColor(ContextCompat.getColor(this,R.color.app_green));
            sold.setCompoundDrawables(null,null,drawable0,null);
            soldNo = 1;
            priceNo = 0;
        }else if (soldNo == 1){
            map.put("sell_type",2);
            map.put("page_index", 1);
            categoryPresenter.categorySearch(map);
            sold.setCompoundDrawables(null,null,drawable1,null);
            soldNo = 2;
        }else {
            map.put("sell_type",1);
            map.put("page_index", 1);
            categoryPresenter.categorySearch(map);
            sold.setCompoundDrawables(null,null,drawable0,null);
            soldNo = 1;
        }
    }
}
